﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ElectronicJournal.Account.EditorAccount
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["id"] == null)
            {
                if (Request.Params["ok"] == null)
                {
                    List<int> years = new DB.DbJournals().GetYears();
                    string html = "<b style=\"font-size: 20px\">Номера журнала</b>";
                    foreach (int year in years)
                    {
                        List<DB.Item.ItemJournal> journals = new DB.DbJournals().GetJournalInYear(year);
                        string htmlString = "<div><table class=\"issue\" cellspacing=\"1\" cellpadding=\"0\"><tbody></tbody></table>";
                        htmlString = htmlString.Insert(htmlString.LastIndexOf("</tbody>"), String.Format("<tr><td style=\"border-right: #aaa 1px solid\">{0}<br><span>год</span></td></tr>", year));
                        for (int i = 0; i < journals.Count; i++)
                        {
                            string adress = "http://" + Request.ServerVariables["HTTP_HOST"] + @"/" + "Account/EditorAccount/Default.aspx?id=" + journals[i].Id.ToString();
                            htmlString = htmlString.Insert(htmlString.LastIndexOf("</tr>"), String.Format("<td onmouseover=\"selectCell(this)\" onmouseout=\"unselectCell(this)\" onclick=\"location.href=\'{0}'\"><a href=\"{0}\">№ {1}</a></td>", adress, journals[i].Number));
                        }
                        html += htmlString;
                    }
                    ContentLabel.Text = html;
                    NavigationLabel.Text = "";
                    PublicationsLinkButton.Visible = false;
                    CreateJournalButton.Visible = true;
                }
                else
                {
                    string str = Request.Params["ok"].ToString();
                    CurrentJournal.Text = str.Substring(Request.Params["ok"].ToString().LastIndexOf("=") + 1);
                    new DB.DbPublications().AddPublicationToJournal(str.Substring(0, str.LastIndexOf("=")), CurrentJournal.Text);
                    Response.Redirect("~/Account/EditorAccount/Default.aspx?id=" + CurrentJournal.Text);
                }
            }
            else
            {
                List<DB.Item.ItemPublication> publications = new DB.DbPublications().GetPublicationsInJournal(Request.Params["id"].ToString());
                CurrentJournal.Text = Request.Params["id"].ToString();
                DB.Item.ItemJournal journal = new DB.DbJournals().GetJournalById(Request.Params["id"].ToString());
                string html = String.Format("<table><tbody><tr><td><b style=\"font-size: 20px\">№ {0} за {1} год</b></td></tr></tbody></table>", journal.Number, journal.Year);
                foreach (DB.Item.ItemPublication publication in publications)
                {
                    html = html.Insert(html.LastIndexOf("</tbody>"), "<tr><td></td></tr>");
                    html = html.Insert(html.LastIndexOf("</td>"), String.Format("<div><div><b>{0}</b></div><div>{1}</div>", publication.TitleRussian, new DB.DbPublications().GetPublicationAuthors(publication.Id), publication.Id));
                }
                ContentLabel.Text = html;
                string adress = "http://" + Request.ServerVariables["HTTP_HOST"] + @"/" + "Account/EditorAccount/Default.aspx";
                NavigationLabel.Text = "<a href=\"" + adress + "\">Номера журналов</a>";
                PublicationsLinkButton.Visible = true;
                CreateJournalButton.Visible = false;
            }
        }

        protected void AddPublicationToJournal(object sender, EventArgs e)
        {
            //
        }

        protected void CreateJournal(object sender, EventArgs e)
        {
            new DB.DbJournals().AddNewJournal();
            Response.Redirect("~/Account/EditorAccount/Default.aspx");
        }

        protected void ShowUnpublishedPublications(object sender, EventArgs e)
        {
            PublicationsLinkButton.Visible = false;
            string adress = "http://" + Request.ServerVariables["HTTP_HOST"] + @"/" + "Account/EditorAccount/Default.aspx";
            NavigationLabel.Text = "<a href=\"" + adress + "\">Номера журналов</a>";
            adress = String.Format("http://" + Request.ServerVariables["HTTP_HOST"] + @"/" + "Account/EditorAccount/Default.aspx?id={0}", CurrentJournal.Text);
            DB.Item.ItemJournal journal = new DB.DbJournals().GetJournalById(CurrentJournal.Text);
            NavigationLabel.Text += String.Format("/<a href=\"" + adress + "\">№ {0} за {1} год</a>", journal.Number, journal.Year);
            List<DB.Item.ItemPublication> list = new DB.DbPublications().GetUnpublishedPublications();
            
            string html = "<table><tbody><tr><td><b style=\"font-size: 20px\">Неопубликованные статьи</b></td></tr></tbody></table>";
            foreach (DB.Item.ItemPublication publication in list)
            {
                html = html.Insert(html.LastIndexOf("</tbody>"), "<tr><td></td></tr>");
                html = html.Insert(html.LastIndexOf("</td>"), String.Format("<div><div><b>{0}</b></div><div>{1}</div><div><a href=\"{2}\">Опубликовать</a></div>", publication.TitleRussian, new DB.DbPublications().GetPublicationAuthors(publication.Id), String.Format("http://" + Request.ServerVariables["HTTP_HOST"] + @"/" + "Account/EditorAccount/Default.aspx?ok={0}={1}", publication.Id, CurrentJournal.Text)));
            }
            ContentLabel.Text = html;
        }
    }
}